﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using QUANLIHOCSINH.BussinessObject;
using QUANLIHOCSINH.DataAccess;
using System.Windows.Forms;
namespace QUANLIHOCSINH.BussinessLogic
{
    class PHANLOPBUS
    {
        BANGDIEMBUS bangdiem = new BANGDIEMBUS();
        ConnectData dataConn = new ConnectData();
        public DataTable LayDanhSachPhanLop(string MaNamHoc, string MaKhoi)
        {
            string sql = "select * from  PhanLop where MaNamHoc ='" + MaNamHoc + "' and MaKhoi='" + MaKhoi + "'";
            return dataConn.GetData(sql);
        }
        public string LaySiSo(string MaNamHoc, string MaKhoi, string MaLop)
        {
            string sql = "select count(MaHocSinh) as SiSo from PhanLop where MaNamHoc = '" + MaNamHoc + "' and MaKhoi ='" + MaKhoi + "' and MaLop='" + MaLop + "'";
            DataTable tbSiSo = dataConn.GetData(sql);
            string siso = "";
            if (tbSiSo.Rows.Count > 0)
            {
                DataRow rowUser = tbSiSo.Rows[0];
                siso = rowUser["SiSo"].ToString();
            }
            return siso;
        }
        public bool KiemTraSiSo(int siso)
        {

            int sisoquidinh = int.Parse(Utility.quidinh.SiSo);
            if (siso < sisoquidinh)
            {
                return true;
            }

            return false;
        }
        public bool ThemPhanLop(PHANLOP hs)
        {
            int siso = int.Parse(LaySiSo(hs.MaNamHoc, hs.MaKhoi, hs.MaLop));

            if (KiemTraSiSo(siso) == true)
            {
                try
                {
                    bangdiem.KhoiTaoBangDiem(hs.MaNamHoc, hs.MaLop, hs.MaHocSinh);
                    siso = siso + 1;
                    //MessageBox.Show(siso.ToString());
                    string updateSiSo = "Update LopHoc set siso ='" + siso + "'where  malop ='" + hs.MaLop + "'";
                    string sql = string.Format("INSERT INTO PHANLOP (manamhoc,makhoi,malop,mahocsinh)  VALUES ('{0}',N'{1}','{2}','{3}')", hs.MaNamHoc, hs.MaKhoi, hs.MaLop, hs.MaHocSinh);

                    dataConn.ExecuteQuery(updateSiSo);
                    if (dataConn.ExecuteQuery(sql))
                    {
                        MessageBox.Show("Thêm Học Sinh Thàng Công", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return true;
                    }

                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
            else
            {
                MessageBox.Show("Qua Si So", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return false;
        }
        public bool SuaPhanLop(PHANLOP hs)
        {
            int siso1 = int.Parse(LaySiSo(hs.MaNamHoc, hs.MaKhoi, hs.MaLop));
            int siso2 = int.Parse(LaySiSo(hs.MaNamHoc, hs.MaKhoi, Utility.MaLop));
            if (hs.MaLop != Utility.MaLop)
            {

                if (KiemTraSiSo(siso1) == true)
                {
                    siso1 = siso1 + 1;
                    siso2 = siso2 - 1;
                    string updateSiSo1 = "Update LopHoc set siso ='" + siso1 + "'where  malop ='" + hs.MaLop + "'";
                    string updateSiSo2 = "Update LopHoc set siso ='" + siso2 + "'where  malop ='" + Utility.MaLop + "'";
                    dataConn.ExecuteQuery(updateSiSo1);
                    dataConn.ExecuteQuery(updateSiSo2);
                    bangdiem.chuyenbangdiem(hs.MaNamHoc, hs.MaLop, hs.MaHocSinh);
                    string sql = string.Format("UPDATE PHANLOP SET MaLop = '{0}'  where mahocsinh = '{1}'", hs.MaLop, hs.MaHocSinh);
                    if (dataConn.ExecuteQuery(sql))
                    {
                        MessageBox.Show("Cập Nhật Học Sinh Thàng Công", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return true;
                    }

                }
                else
                {
                    MessageBox.Show("Qua Si So", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return true;
            }
            return false;
        }

        public void XoaPhanLop(PHANLOP hs)
        {
            int siso = int.Parse(LaySiSo(hs.MaNamHoc, hs.MaKhoi, hs.MaLop));
          
                siso = siso - 1;
               // MessageBox.Show(siso.ToString());
                string updateSiSo = "Update LopHoc set siso ='" + siso + "' where  malop ='" + hs.MaLop + "'";
                //MessageBox.Show(updateSiSo);
                dataConn.ExecuteQuery(updateSiSo);
                string sql = string.Format("DELETE  FROM PhanLop Where mahocsinh = '{0}'", hs.MaHocSinh);
                bangdiem.xoabangdiem(hs.MaNamHoc, hs.MaLop, hs.MaHocSinh);
                if (dataConn.ExecuteQuery(sql))
                {
                    MessageBox.Show("Thêm Học Sinh Thàng Công", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            

        }
    }
}
